import { computed } from "vue";
import { useRouter } from "vue-router";
import { post } from "./post";

export const useNewAddress = (list, changeMessage,url) => {
    const router = useRouter();
    const data = computed(() => {
        let data = [];
        for (let i = 0; i < list.length; i++) {
            data[i] = list[i].result;
        }
        return data;
    });
    // 设置getData方法发送修改或新建的地址信息
    const getData = async () => {
        const result = await post(url, {
            city: data.value[0],
            department: data.value[1],
            houseNumber: data.value[2],
            name: data.value[3],
            phone: data.value[4]
        });
        if (result?.errno === 0 && result?.data) {
            changeMessage("保存成功");
        } else {
            changeMessage("保存失败");
        }
    };
    const handlePreserve = () => {
        getData();
    };
    const handleBack = () => {
        router.back();
    };
    // 返回data，handleBack点击返回方法，handlePreserve点击保存方法
    return { handleBack, data, handlePreserve };
};